package com.zd.wechatPlatform.service.impl;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.zd.wechatPlatform.common.enums.WeixinEventTypeEnum;
import com.zd.wechatPlatform.common.enums.WeixinMsgTypeEnum;
import com.zd.wechatPlatform.service.IWeixinMsgHandler;

/**
 * 微信消息处理器
 * 
 * @author lishi
 * @date 2018年11月1日 上午10:16:04
 */
@Service("weixinMsgHandler")
public class WeixinMsgHandler implements IWeixinMsgHandler {
	@Autowired
	@Qualifier("clickEventMsgHandler")
	private IWeixinMsgHandler clickEventMsgHandler;

	@Autowired
	@Qualifier("imageMsgHandler")
	private IWeixinMsgHandler imageMsgHandler;

	@Autowired
	@Qualifier("linkMsgHandler")
	private IWeixinMsgHandler linkMsgHandler;

	@Autowired
	@Qualifier("locationEventMsgHandler")
	private IWeixinMsgHandler locationEventMsgHandler;

	@Autowired
	@Qualifier("locationMsgHandler")
	private IWeixinMsgHandler locationMsgHandler;

	@Autowired
	@Qualifier("scanEventMsgHandler")
	private IWeixinMsgHandler scanEventMsgHandler;

	@Autowired
	@Qualifier("shortvideoMsgHandler")
	private IWeixinMsgHandler shortvideoMsgHandler;

	@Autowired
	@Qualifier("subscribeEventMsgHandler")
	private IWeixinMsgHandler subscribeEventMsgHandler;

	@Autowired
	@Qualifier("textMsgHandler")
	private IWeixinMsgHandler textMsgHandler;

	@Autowired
	@Qualifier("unsubscribeEventMsgHandler")
	private IWeixinMsgHandler unsubscribeEventMsgHandler;

	@Autowired
	@Qualifier("videoMsgHandler")
	private IWeixinMsgHandler videoMsgHandler;

	@Autowired
	@Qualifier("viewEventMsgHandler")
	private IWeixinMsgHandler viewEventMsgHandler;

	@Autowired
	@Qualifier("voiceMsgHandler")
	private IWeixinMsgHandler voiceMsgHandler;

	@Override
	public void handlerMsg(Map<String, String> msgMap) throws Exception {
		getMsgHandler(msgMap).handlerMsg(msgMap);
	}

	private IWeixinMsgHandler getMsgHandler(Map<String, String> msgMap) throws Exception{
		String msgType = (String) msgMap.get("MsgType");
		String eventType = "";
		if (msgMap.get("Event") != null) {
			eventType = (String) msgMap.get("Event");
		}

		if (WeixinMsgTypeEnum.event.getKey().equals(msgType)) {
			if(WeixinEventTypeEnum.subscribe.getKey().equals(eventType)) {
				return subscribeEventMsgHandler;
			}
			if(WeixinEventTypeEnum.unsubscribe.getKey().equals(eventType)) {
				return unsubscribeEventMsgHandler;
			}
			if(WeixinEventTypeEnum.SCAN.getKey().equals(eventType)) {
				return scanEventMsgHandler;
			}
			if(WeixinEventTypeEnum.LOCATION.getKey().equals(eventType)) {
				return locationEventMsgHandler;
			}
			
			if(WeixinEventTypeEnum.CLICK.getKey().equals(eventType)) {
				return clickEventMsgHandler;
			}
			if(WeixinEventTypeEnum.VIEW.getKey().equals(eventType)) {
				return viewEventMsgHandler;
			}
		}

		if (WeixinMsgTypeEnum.text.getKey().equals(msgType)) {
			return textMsgHandler;
		}
		if (WeixinMsgTypeEnum.image.getKey().equals(msgType)) {
			return imageMsgHandler;
		}
		if (WeixinMsgTypeEnum.voice.getKey().equals(msgType)) {
			return voiceMsgHandler;
		}

		if (WeixinMsgTypeEnum.video.getKey().equals(msgType)) {
			return videoMsgHandler;
		}
		if (WeixinMsgTypeEnum.shortvideo.getKey().equals(msgType)) {
			return shortvideoMsgHandler;
		}
		if (WeixinMsgTypeEnum.location.getKey().equals(msgType)) {
			return locationMsgHandler;
		}
		if (WeixinMsgTypeEnum.link.getKey().equals(msgType)) {
			return linkMsgHandler;
		}
		
		throw new Exception("无法解析的消息类型");
	}
}
